package com.banciyuan.bcywebview.biz.privatemessage;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.SpaceApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.AppSetting;

/**
 * Created by squall on 2016/9/12.
 */
public class PrivateMessageSettingActivity extends BaseActivity {


    private ImageView select_one_iv;
    private ImageView select_two_iv;
    private RelativeLayout select_one_rl;
    private RelativeLayout select_two_rl;

    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private RequestQueue queue;

    private AppSetting appSetting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privatemessage_setting);
        initArgs();
        initActionbar();
        initUi();
        initData();
        initAction();
    }

    @Override
    protected void initArgs() {
        queue = VolleyQueue.getRquest(this);
        appSetting = (AppSetting) getIntent().getSerializableExtra(gotoUtil.INTENT_VALUE_ONE);
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.private_setting));
    }

    @Override
    protected void initUi() {
        select_one_iv = (ImageView) findViewById(R.id.select_one_iv);
        select_two_iv = (ImageView) findViewById(R.id.select_two_iv);
        select_one_rl = (RelativeLayout) findViewById(R.id.rl_select_one);
        select_two_rl = (RelativeLayout) findViewById(R.id.rl_select_two);
    }

    @Override
    protected void initAction() {
        select_one_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (select_one_iv.getVisibility() == View.VISIBLE) return;
                changeSet("follow");
            }
        });

        select_two_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (select_two_iv.getVisibility() == View.VISIBLE) return;
                changeSet("all");
            }
        });
    }


    @Override
    protected void initData() {
        if (appSetting != null) {
            renderData();
        } else {
            String url = HttpUtils.BASE_URL + SpaceApi.getSettings();
            List<BcyPair> pairs = new ArrayList<>();
            pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
            pairs.add(new BcyPair(HttpUtils.STYPE, "message"));
            Map<String, String> map = HttpUtils.getData(pairs);
            Response.Listener<String> onSucc = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        if (StatusCodeUtils.dealStatusCode(response, PrivateMessageSettingActivity.this)) {
                            JSONObject jsonObject = new JSONObject(response);
                            Gson gson = new Gson();
                            appSetting = gson.fromJson(jsonObject.getString(HttpUtils.UPLOAD_DATA), AppSetting.class);
                            renderData();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            Response.ErrorListener onError = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            };

            OnErrorProxy errorProxy = new OnErrorProxy(onError, onSucc, url, this, map);
            StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, errorProxy);
            VolleyQueue.getRquest(this).add(requestParam);
        }
    }

    private void renderData() {
        if (appSetting != null && appSetting.getMessage() != null) {
            String type = appSetting.getMessage().getSend();
            if (StringUtil.notNullEqual(type, "all")) {
                select_one_iv.setVisibility(View.GONE);
                select_two_iv.setVisibility(View.VISIBLE);
            } else {
                select_one_iv.setVisibility(View.VISIBLE);
                select_two_iv.setVisibility(View.GONE);
            }
        } else {

        }
    }

    private void changeSet(final String type) {
        String url = HttpUtils.BASE_URL + SpaceApi.getSetMessage();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        pairs.add(new BcyPair(HttpUtils.TYPE, "send"));
        pairs.add(new BcyPair(HttpUtils.UPLOAD_STATUS, type));
        Map<String, String> map = HttpUtils.getData(pairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, PrivateMessageSettingActivity.this)) {
                    if (type.equals("follow")) {
                        select_one_iv.setVisibility(View.VISIBLE);
                        select_two_iv.setVisibility(View.GONE);
                    } else {
                        select_one_iv.setVisibility(View.GONE);
                        select_two_iv.setVisibility(View.VISIBLE);
                    }
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        };

        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(requestParam);
    }

}
